#include<../data_tools/usefull_routines.c>

int main(int argc, char *argv[])
{
  int i,ndata;
  double *Sigma,*kappa,sigmacri,*disVR,*R,dummy,Q;
  char *infile,infiles[1000],outfiles[1000];

  FILE *density,*Kappa,*dispersion;

  printf("\nTAKE CARE!!!\n");

  infile = argv[1];
  sprintf(infiles,"%s",infile);
  ndata = contadorParticulas(infiles);
  printf("\nthe files:\n%s data = %d\n",infiles,ndata);
  density = fopen(infiles,"r");
  Sigma = (double *)malloc((size_t)ndata*sizeof(double));
  if(Sigma == NULL){
    printf("Allocation error routines:81\n");
    exit(0);
  }

  infile = argv[2];
  sprintf(infiles,"%s",infile);
  ndata = contadorParticulas(infiles); 
  printf("%s data = %d\n",infiles,ndata); 
  Kappa = fopen(infiles,"r");
  kappa = (double *)malloc((size_t)ndata*sizeof(double));
  if(kappa == NULL){
    printf("Allocation error routines:81\n");
    exit(0);
  }

  infile = argv[3];
  sprintf(infiles,"%s",infile);
  ndata = contadorParticulas(infiles);
  printf("%s data = %d\n",infiles,ndata); 
  dispersion = fopen(infiles,"r");
  disVR = (double *)malloc((size_t)ndata*sizeof(double));
  if(disVR == NULL){
    printf("Allocation error routines:81\n");
    exit(0);
  }

  printf("must have the same number of data.\n\n");
  
  R = (double *)malloc((size_t)ndata*sizeof(double));
  if(R == NULL){
    printf("Allocation error routines:81\n");
    exit(0);
  }

  sprintf(outfiles,"Toomre_%s",infiles);
  FILE *Toomre;
  Toomre = fopen(outfiles,"w");
  int count=0; 
  for(i=0; i<ndata; i++)
    {
      fscanf(density,"%lf %lf %lf %lf %lf %lf",&R[i],&Sigma[i],&dummy,&dummy,&dummy,&dummy);
      printf("%e %e\t",R[i],Sigma[i]);
      fscanf(dispersion,"%lf %lf %lf %lf %lf",&R[i],&disVR[i],&dummy,&dummy,&dummy);
      printf("%e %e\t",R[i],disVR[i]);
      if((i>=2) && (i<ndata-2))
	{
	  fscanf(Kappa,"%lf %lf %lf %lf %lf %lf %lf",&R[i-2],&dummy,&dummy,&dummy,&dummy,&kappa[i-2],&dummy);
	  printf("%e %e\t",R[i-2],kappa[i-2]);
	  if(kappa[i-2]>0.0)
	    {
	      sigmacri = (3.36*GGAD*Sigma[i])/kappa[i-2];
	      Q = disVR[i]/sigmacri;
	      
	      fprintf(Toomre,"%e %e %e\n",R[i-2],sigmacri,Q);
	    }
	  count++;
	  printf("%d %d\t",i-2,count);
	}
      printf("\n");
    }


  printf("Done\n.");

  fclose(density);
  fclose(Kappa);
  fclose(dispersion);
  fclose(Toomre);
  return 0;
}
